package org.iku.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.iku.dao.ItemIndexDAO;
import org.iku.entity.Category;

public class CategoryTool {
	
	
	private static Map<Integer,Category> categoryMap;
	private static Map<String,Integer> invertedMap;
	
	public static void init(ItemIndexDAO itemIndexDAO){
		
		List<Category> categories = itemIndexDAO.getAllCategories();
		
		if(null == categoryMap || categoryMap.size()<=0){
			categoryMap = new HashMap<Integer, Category>();
		}
		
		if(null == invertedMap || invertedMap.size()<=0){
			invertedMap = new HashMap<String, Integer>();
		}
		
		for(Category category : categories){
			categoryMap.put(category.getId(), category);
			invertedMap.put(category.getNameCn(), category.getId());
			invertedMap.put(category.getNameEn(), category.getId());
		}
	}
	
	public static Category getCategory(int id){
		return categoryMap.get(id);
	}
	
	public static boolean containCategory(String name){
		return invertedMap.containsKey(name);
	}
	
	public static Category getCategory(String name){
		Integer id = invertedMap.get(name);
		return categoryMap.get(id);
}
	
	public static void addCategory(Category category){
		categoryMap.put(category.getId(), category);
		invertedMap.put(category.getNameCn(), category.getId());
		invertedMap.put(category.getNameEn(), category.getId());
	}
	
}
